﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using Model;

namespace ACM
{
    public partial class ACM : System.Web.UI.MasterPage
    {
        public int MyReq { get; set; }
        public int ReqToMe { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblCurUsr.Text = CurrentUser.user_name;

            ShowControlWith(lnkSaleSearch, "dealer");
            ShowControlWith(lnkSaleKeyIn, "dealer");
            ShowControlWith(lnkChangeDealer, "dealer");
            ShowControlWith(lnkRoSearch, "ro");
            ShowControlWith(lnkTMTSearch, "tmt");
            ShowControlWith(lnkUpload, "tmt");

            GetNotificationRequest();
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session.Remove("CurrentUser");
            Response.Redirect("Default.aspx");
        }

        public acm_user CurrentUser
        {
            get
            {
                if (Session["CurrentUser"] == null)
                    Response.Redirect("Default.aspx");
                return (acm_user)Session["CurrentUser"];
            }
            set
            {
                Session["CurrentUser"] = value;
            }
        }

        protected void HideControlWith(Control ctrl, string role)
        {
            ctrl.Visible = !(CurrentUser.user_role == role);
        }

        protected void ShowControlWith(Control ctrl, string role)
        {
            ctrl.Visible = (CurrentUser.user_role == role);
        }

        protected void GetNotificationRequest()
        {
            SearchRequestCommand cmd = new SearchRequestCommand(CurrentUser.dealer_code, "REQ_BY_U");
            cmd.Execute();

            SearchRequestCommand cmd2 = new SearchRequestCommand(CurrentUser.dealer_code, "REQ_TO_U");
            cmd2.Execute();

            MyReq = (cmd.Result != null) ? cmd.Result.Count : 0;
            ReqToMe = (cmd2.Result != null) ? cmd2.Result.Count : 0;

            int a = MyReq + ReqToMe;

            lblNotification.Text = (a > 0) ? a.ToString() : "";
        }
    }
}